import borg
import threading
import safethread
import time

class Storage(borg.Borg, safethread.SafeThread):
	def __init__(self):
		borg.Borg.__init__(self, self.init)

	def init(self):
		# Actualy, that's constructor
		safethread.SafeThread.__init__(self)
		self.lastSync = time.time()
		self.interval = 60

		# Now we start the thread
		self.start()

	def setFlushInterval(self, seconds):
		self.interval = seconds
		return self

	def step(self):
		time.sleep(1)
		if (self.lastSync + self.interval < time.time()) and (self.interval != 0):
			self.flush()

	def flush(self):
		print 'Flushing'
		self.lastSync = time.time()
		return self
